package com.eu.ebpm.engine.express;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author wxh
 */
public class Express {
	
	 /**
	  * 例如：
	  * 	day > 3 || day < 2 && apple='argee' || result = 'apple' || day == 1 && result == 'little' && aply=='employ' || (result = 'apply').size() > 2
	  */
	private String express;
	
	private List<ExpressItem> condtionItems = new ArrayList<ExpressItem>();

	public Express(String express) {
		this.express = express;
		for (String expItem : express.split("\\|\\|")) {
			condtionItems.add(new ExpressItem(expItem));
		}
	}
	
	/**
	 * 是否匹配
	 * @param condition 条件
	 * @return 匹配结果
	 */
	public boolean isMate(Map<String, Object> condition) {
		
		// 1、判断key-value的值是否跟express完全一致；
		for (Map.Entry<String, Object> entry : condition.entrySet()) {
			Object value = entry.getValue();
			if (value instanceof String && entry.getKey().equals(value) && this.express.equals(value)) {
				return true;
			}
		}

		// 2、判断conditionItems是否有匹配
		for (ExpressItem item : this.condtionItems) {
			if (item.isMate(condition)) {
				return true;
			}
		}
		
		return false;
	}
	
}
